<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="account"
					type="text"
					maxlength="12"
					placeholder="用户名"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					maxlength="16"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>	
			<!-- 底部信息 -->
			<view class="footer">
		<!-- 		<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text> -->
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let that;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage:'https://qiniu.hiima.cn/98c312cfd249cd0de18c5d76cded4bfd9f03ceea1617267130.png',
				account:'', //用户/电话
				password:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			that= this;
			//this.isLogin();
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		that.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
		    startLogin(e){
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				// if (this.account.length<6) {
				//      uni.showToast({
				//         icon: 'none',
				// 				position: 'bottom',
				//         title: '用户名格式错误'
				//     });
				//      return;
				// }
		    if (this.password.length < 6) {
						 uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: '密码格式错误'
						 });
							return;
		        }
				that.isRotate=true
				this.$request.httpTokenRequest({
					url: '/admin/login/login',
					method: 'POST',
					data: {
						account: that.account,
						password: that.password,
					},
					needToken:false,
				}).then(res => {
					if (res.data.code === 200) {
						//登陆成功保存登录信息
						uni.setStorageSync('token', res.data.content.token);
						uni.setStorageSync('userID',res.data.content.userInfo.id);
						uni.setStorageSync('userInfo',res.data.content.userInfo);
						uni.setStorageSync('rule',res.data.content.userInfo.rule);
						that.isRotate=false;
						//登录成功，权限判断
						switch(res.data.content.userInfo.rule){
							case 1 : //商户
							uni.showToast({
							    icon:'success' , 
								  position: 'bottom',
							    title: '登录成功'
							});
							var timer=setTimeout(()=>{
								uni.reLaunch({
									url: '/pages/index/merchant-index' 
								});
								timer=null
							},1000);
							break;
							case 2 : //代理商
							uni.showToast({
							    icon:'success' , 
								  position: 'bottom',
							    title: '登录成功'
							});
							var timer=setTimeout(()=>{
								uni.reLaunch({
									url: '/pages/index/agent-index'
								});
								timer=null
							},1000);
							break;
							default:
							uni.showToast({
							    icon:'none' ,
								  position: 'bottom',
								  duration: 3000,
							    title: '该账号不是代理商或者商户！请查证以后再登录'
							});
						}
					}else{
						that.isRotate=false;
						uni.showToast({
						    icon:'none' ,
								position: 'bottom',
						    title: res.data.msg
						});
					}
				}, error => {
				});
				// uni.showLoading({
				// 	title: '登录中'
				// });
				// getLogin()
				// .then(res => {
				// 	//console.log(res)
				// 	//简单验证下登录（不安全）
				// 	if(that.account==res.data.username && that.passData==res.data.password){
				// 		let userdata={
				// 			"username":res.data.username,
				// 			"nickname":res.data.nickname,
				// 			"accesstoken":res.data.accesstoken,
				// 		} //保存用户信息和accesstoken
				// 		that.$store.dispatch("setUserData",userdata); //存入状态
				// 		try {
				// 			uni.setStorageSync('setUserData', userdata); //存入缓存
				// 		} catch (e) {
				// 			// error
				// 		}
				// 		uni.showToast({
				// 			icon: 'success',
				// 			position: 'bottom',
				// 			title: '登录成功'
				// 		});
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}else{
				// 		that.passData=""
				// 		uni.showToast({
				// 			icon: 'error',
				// 			position: 'bottom',
				// 			title: '账号或密码错误，账号admin密码admin'
				// 		});
				// 	}
				// 	uni.hideLoading();
				// }).catch(err => {
				// 	uni.hideLoading();
				// })
				
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	.content {
		display: flex;
		flex-direction: column;
		justify-content:center;
		/* margin-top: 128rpx; */
	}
	
	/* 头部 logo */
	.header {
		width:161rpx;
		height:161rpx;
		box-shadow:0rpx 0rpx 60rpx 0rpx rgba(0,0,0,0.1);
		/* border-radius:50%; */
		/* background-color: #000000; */
		margin-top: 128rpx;
		margin-bottom: 72rpx;
		margin-left: auto;
		margin-right: auto;
	}
	.header image{
		width:161rpx;
		height:161rpx;
		/* border-radius:50%; */
	}
	
	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		padding-left: 70rpx;
		padding-right: 70rpx;
	}
	.tips {
		color: #999999;
		font-size: 28rpx;
		margin-top: 64rpx;
		margin-left: 48rpx;
	}
	
	/* 登录按钮 */
	.wbutton{
		margin-top: 96rpx;
	}
	
	/* 其他登录方式 */
	.other_login{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256rpx;
		text-align: center;
	}
	.login_icon{
		border: none;
		font-size: 64rpx;
		margin: 0 64rpx 0 64rpx;
		color: rgba(0,0,0,0.7)
	}	
	/* 底部 */
	.footer{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		margin-top: 64rpx;
		color: rgba(0,0,0,0.7);
		text-align: center;
		height: 40rpx;
		line-height: 40rpx;
	}
	.footer text{
		font-size: 24rpx;
		margin-left: 15rpx;
		margin-right: 15rpx;
	}
</style>
